// src/eventBusManager.ts
import mitt from 'mitt';

type EventBusEvents = {
  [key: string]: any; // 定义事件的类型，可以根据实际的事件来具体定义
};

class EventBusManager {
  private static instance: EventBusManager;
  private emitter = mitt<EventBusEvents>();

  // 单例模式，确保只有一个 EventBusManager 实例
  private constructor() {}

  // 获取单例实例
  public static getInstance(): EventBusManager {
    if (!EventBusManager.instance) {
      EventBusManager.instance = new EventBusManager();
    }
    return EventBusManager.instance;
  }

  // 触发事件
  public emit(event: string, payload: any): void {
    this.emitter.emit(event, payload);
  }

  // 监听事件
  public on(event: string, handler: (payload: any) => void): void {
    this.emitter.on(event, handler);
  }

  // 取消事件监听
  public off(event: string, handler?: (payload: any) => void): void {
    this.emitter.off(event, handler);
  }
}

export default EventBusManager;
